<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Account extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form','url','debug','language','string'));
		$this->load->library('form_validation');
		$this->load->model("account_model");
		$this->config->set_item('language', 'english-us');
	}

	public function login(){

		$this->load->view("account/header");
		$this->load->view("account/login");
		$this->load->view("account/footer");
	}

	public function do_login()
	{
		$this->form_validation->set_rules('txtEmail', 'Username', 'required|valid_email');
		$this->form_validation->set_rules('txtPassword', 'Password', 'required|min_length[6]|max_length[12]');
		if ($this->form_validation->run() == FALSE)
		{
			$this->login();
		}
		else
		{
			$param = array();
			if($this->input->post("txtEmail"))
			{
				$param['email'] = $this->input->post("txtEmail");
			}
			if($this->input->post("txtPassword"))
			{
				$param['password'] = sha1($this->input->post("txtPassword"));
			}
			
			$result = array();
			$result = $this->account_model->login($param);
			
			if(count($result) > 0)
			{
				$this->session->set_userdata($result[0]);
				redirect("/member/en-us/home/");
			}
			else
			{
				$data = array();
				$data['login_failiure'] = "email and password does not match please try again.";
				$this->load->view("account/header");
				$this->load->view("account/login",$data);
				$this->load->view("account/footer");
			}
			
		}
	}

	public function logout()
	{
		$this->session->sess_destroy();
		redirect("/");
	}

	public function register() {

		$this->lang->load('register');

		$this->load->view("account/header");
		$this->load->view("account/register");
		$this->load->view("account/footer");
	}

	public function register_success()
	{
		$this->load->view("account/header");
		$this->load->view("account/register_success");
		$this->load->view("account/footer");
	}

	public function register_failiure()
	{
		$this->load->view("account/header");
		$this->load->view("account/register_failiure");
		$this->load->view("account/footer");
	}

	public function do_register()
	{
		$this->lang->load('register');
		$this->_do_register_validation();

		// Need to add +3 hours in current time
		// for approval_request_expiration
		$date = new DateTime();
		$date = $date->add(new DateInterval('P1D'));

		//var_dump($date);

		$param = array();
		$param['fullname'] = $this->input->post('txtName');
		$param['user_type_id'] = '1';
		$param['email']  = $this->input->post('txtEmail');
		$param['password'] = sha1($this->input->post('txtPassword'));
		$param['register_date'] = date("Y-m-d");
		$param['language_id'] = '3';
		$param['approval_code'] = random_string('alnum',12);
		$param['approval_request_expiration'] = $date->format('Y-m-d H:i:s');
		$insertID = $this->account_model->register($param);
		if($insertID > 0)
		{
			$this->register_success();
		}
		else
		{
			$this->register_failiure();
		}
	}

	private function _do_register_validation()
	{
		$this->form_validation->set_rules('txtName', 'Username', 'required');
		$this->form_validation->set_rules('txtEmail', 'Email', 'valid_email');
		$this->form_validation->set_rules('txtPassword', 'Password', 'required|min_length[6]|max_length[12]');
		$this->form_validation->set_rules('txtConfirmPassword', 'Confirm Password', 'required|matches[txtPassword]');

		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view("account/header");
			$this->load->view("account/register");
			$this->load->view("account/footer");

			return false;
		}

		return true;
	}

	public function forgot_password(){
	}

	public function do_forgot_password(){
	}

	public function approve_user(){
	}

	public function change_password(){
	}

	public function do_change_password(){
	}

}